package com.leo.springbootmultithreadbasic.lock.reentranreadwritelock.readwrite;

/**
 * @author Leo
 * @version V1.0
 * @date 2021/11/5 10:31 PM
 */
public class MainDemo {
    public static void main(String[] args) {
//        testReadWrite();
        testWriteRead();
    }


    /**
     * Test Result:
     * 同一时刻，只允许一个线程执行lock()方法后面的代码，体现了读锁写锁之间的互斥
     *
     *
     */
    public static void testReadWrite() {
        MyService myService = new MyService();

        // 读
        Thread threadA = new Thread(new MyRunnableA(myService));
        threadA.setName("A");


        // 写
        Thread threadB = new Thread(new MyRunnableB(myService));
        threadB.setName("B");

        threadA.start();
        threadB.start();

    }

    /**
     * Test Result:
     * 同一时刻，只允许一个线程执行lock()方法后面的代码，体现了写锁读锁之间的互斥
     *
     *
     */
    public static void testWriteRead() {
        MyService myService = new MyService();

        // 读
        Thread threadB = new Thread(new MyRunnableB(myService));
        threadB.setName("B");


        // 写
        Thread threadA = new Thread(new MyRunnableA(myService));
        threadA.setName("A");

        threadB.start();
        threadA.start();

    }


}
